using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;

public class Board : MonoBehaviour
{
    public Tilemap Tilemap { get; private set; }

    public Tile tileUnknow;
    public Tile tileEmpty;
    public Tile tileFlag;
    public Tile tileMine;
    public Tile tileExploded;
    public Tile[] tileNumber;
    public Color highlightColor;
    private Vector3Int lastHighlightedCell = new(-1, -1, 0);
    private Game parentGame;
    private void Awake()
    {
        Tilemap = GetComponent<Tilemap>();
    }
    void Start()
    {
        parentGame = GetComponentInParent<Game>(false);
        StartCoroutine(HightLightUpdate());
    }
    private IEnumerator HightLightUpdate()
    {
        while (true)
        {
            HighlightCellUnderMouse();
            yield return new WaitForSeconds(0.02f);
        }
    }
    //绘制方法，根据提供的地图大小绘制
    public void Draw(Cell[,] state)
    {
        int width = state.GetLength(0);
        int height = state.GetLength(1);

        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                Cell cell = state[x, y];
                //绘制具体的位置的图片
                Tilemap.SetTile(cell.position, GetTile(cell));
            }
        }
    }
    // 获取需要绘制的类型
    private Tile GetTile(Cell cell)
    {
        if (cell.revealed)
        {
            return GetRevealdTile(cell);
        }
        else if (cell.flagged)
        {
            return tileFlag;
        }
        else
        {
            return tileUnknow;
        }
    }

    // 获取被点击后的类型
    private Tile GetRevealdTile(Cell cell)
    {
        return cell.type switch
        {
            Cell.Type.Empty => tileEmpty,
            Cell.Type.Mine => cell.exploded ? tileExploded : tileMine,
            Cell.Type.Number => tileNumber[cell.number - 1],
            _ => null,
        };
    }
    private void HighlightCellUnderMouse()
    {
        // 将鼠标位置转换为世界坐标
        Vector3 mouseWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        // 获取当前鼠标所在的单元格坐标
        Vector3Int cellPosition = Tilemap.WorldToCell(mouseWorldPos);
        // 如果鼠标移出棋盘或单元格未变化，则跳过
        if (cellPosition == lastHighlightedCell) return;
        // 恢复上一次高亮的单元格颜色
        if (IsValidCell(lastHighlightedCell)) Tilemap.SetColor(lastHighlightedCell, Color.white);
        // 高亮新单元格
        if (IsValidCell(cellPosition))
        {
            ApplyCellHighlight(cellPosition);
            lastHighlightedCell = cellPosition;
        }
        else
        {
            lastHighlightedCell = new Vector3Int(-1, -1, 0);
        }
    }

    private bool IsValidCell(Vector3Int cellPos)
    {
        // 检查坐标是否在棋盘范围内
        return cellPos.x >= 0 && cellPos.x < parentGame.width && cellPos.y >= 0 && cellPos.y < parentGame.height;
    }

    private void ApplyCellHighlight(Vector3Int cellPos)
    {
        Tilemap.SetTileFlags(cellPos, TileFlags.None);
        Tilemap.SetColor(cellPos, highlightColor);
    }
}
